/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-12-8 上午11:38 Created by dwzhang
 */
package com.jade.modules.message.store.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import com.jade.modules.message.store.MessageDao;
import com.jade.modules.message.store.MessageRecord;
import com.jade.modules.message.store.MessageStore;
import com.jade.modules.message.store.StoreException;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消息记录存储器实现。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-12-8 上午11:38
 */
@Named ("module_messageStore")
public class MessageStoreImpl
        implements MessageStore
{
    private MessageDao messageDao;

    @Inject
    public void setMessageDao(@Named ("module_messageDao") MessageDao messageDao)
    {
        this.messageDao = messageDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void add(MessageRecord message)
            throws StoreException
    {
        messageDao.add(message);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void updateStatus(long id, int status)
            throws StoreException
    {
        MessageRecord message = get(id);
        if (message != null) {
            message.setStatus(status);
            messageDao.update(message);
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void remove(long... ids)
    {
        for (long id : ids) {
            MessageRecord message = get(id);
            if (message != null) {
                messageDao.remove(message);
            }
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public int remove(String receiverType, String receiver, String category)
    {
        return messageDao.remove(receiverType, receiver, category);
    }

    public MessageRecord get(long id)
    {
        return messageDao.get(id);
    }

    public PagingList<MessageRecord> query(Criteria criteria)
    {
        return messageDao.query(criteria);
    }

    public List<MessageRecord> list(Criteria criteria)
    {
        return messageDao.list(criteria);
    }

    public long getNewMsgCount(String receiverType, String receiver, Date time)
    {
        return getNewMsgCount(null, receiverType, receiver, time);
    }

    public long getNewMsgCount(String category, String receiverType, String receiver, Date time)
    {
        return messageDao.getNewMsgCount(category, receiverType, receiver, time);
    }

    public Map<String, Long> getNewMsgCountByCategory(String receiverType, String receiver, Date time)
    {
        return messageDao.getNewMsgCountByCategory(receiverType, receiver, time);
    }
}
